/* 学习目标：掌握-修改stata中的数据  */

import { legacy_createStore as createStore } from 'redux';

// 2. reducer的第二个形参，接收action
function xxxReducer(state = { count: 0, msg: 'hello' }, action) {
  if (action.type === 'add') {
    // 3. reducer根据action计算新值，修改state， 返回新的state
    // 不可变数据原则：新值覆盖旧值
    return {
      ...state,
      count: state.count + 1,
    };
  }

  if (action.type === 'des') {
    return {
      ...state,
      count: state.count - 1,
    };
  }
  return state;
}
const store = createStore(xxxReducer);
console.log('store.getState()  ----->  ', store.getState());

// 1. dispatch触发一个action：dispatch(action)
// 💥💥action 是一个对象，必须有type属性
store.dispatch({ type: 'add' });
console.log('store.getState()  ----->  ', store.getState());

store.dispatch({ type: 'des' });
console.log('store.getState()  ----->  ', store.getState());
